package cn.biplam.common.tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * 操作系统工具类
 * @author WangSong
 * @create Mar 8, 2009 6:50:49 PM
 * @version 1.0
 */
public abstract class SYSTEM {

	/**
	 * 使执行等待一段时间
	 * @param millis 等待毫秒数
	 */
	public static void sleep(long lMillis){
		try {
			Thread.sleep(lMillis);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 获取类路径
	 * @return
	 */
	public static String getClassPath(){
		String path = Thread.currentThread().getContextClassLoader().getResource("").getPath();
		return path;
	}
	
	/**
	 * 获取用户的当前工作目录
	 * @return
	 */
	public static String getProjectPath(){
		return System.getProperty("user.dir");
	}
	
	   public static String getMACAddress() {   
		   
	         String address = "";   
	         String os = System.getProperty("os.name");   
	        if (os != null) {   
	            if (os.startsWith("Windows")) {   
	                try {   
	                     ProcessBuilder pb = new ProcessBuilder("ipconfig", "/all");   
	                     Process p = pb.start();   
	                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));   
	                     String line;   
	                    while ((line = br.readLine()) != null) {   
	                        if (line.indexOf("Physical Address") != -1) {   
	                            int index = line.indexOf(":");   
	                             address = line.substring(index + 1);   
	                            break;   
	                         }   
	                     }   
	                     br.close();   
	                    return address.trim();   
	                 } catch (IOException e) {   
	                       
	                 }   
	             }else if(os.startsWith("Linux")){   
	                try {   
	                     ProcessBuilder pb = new ProcessBuilder("ifconfig");   
	                     Process p = pb.start();   
	                     BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream()));   
	                     String line;   
	                    while((line=br.readLine())!=null){   
	                        int index=line.indexOf("硬件地址");   
	                        if(index!=-1){   
	                             address=line.substring(index+4);   
	                            break;   
	                         }   
	                     }   
	                     br.close();   
	                    return address.trim();   
	                 } catch (IOException ex) {   
	                     Logger.getLogger(SYSTEM.class.getName()).log(Level.SEVERE, null, ex);   
	                 }   
	                   
	             }   
	         }   
	        return address;   
	     }   
}
